//
//  HWGameCircleCell.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/12/30.
//

import UIKit
class HWGameCircleCell: UICollectionViewCell {
    var bgView = c_make_ex(UIView.self)
        .bgColor(.white).end()
    var imageView:UIImageView!
    var playImage:UIImageView!
    let delButton = c_make_ex(UIButton.self)
        .image("Push_delete").end()
    var model:HWGameCircleModel?{
        didSet {
            if model?.video.count ?? 0 > 0 {
                playImage.isHidden = false
                if model?.img.count ?? 0 > 0 {
                    self.imageView.sd_setImage(with: URL(string: model!.img.first!), completed: nil)
                }else{
                    self.imageView.image = nil
                }
            }else {
                playImage.isHidden = true
            }
        }
    }
    
    var videoUrl:String?{
        didSet {
            
            
            
        }
    }
    
    var index = 0
    var delBlock: ((_ delIndex: Int) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameCircleCell{
    private func initUI() -> Void {
        
//        backgroundColor = .clear
//        contentView.backgroundColor = .clear
        
        // 可选：设置 contentView 的背景颜色以便于对比
        self.contentView.backgroundColor = UIColor.white
        
        imageView = UIImageView.init()
        imageView.backgroundColor = UIColor.clear
        imageView.layer.masksToBounds = true
        imageView.contentMode = UIView.ContentMode.scaleAspectFill
        contentView.addSubview(imageView)
        
        playImage = UIImageView()
        playImage.image = UIImage(named: "playVideo")
        imageView.addSubview(playImage)
        playImage.isHidden = true
        
        delButton.isHidden = true
        delButton.addTarget(self, action: #selector(clickDelButton), for: .touchUpInside)
        contentView.addSubview(delButton)
    }
    
    private func initFrame() -> Void {
        
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        playImage.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        delButton.snp.makeConstraints { make in
            make.top.trailing.equalToSuperview()
            make.width.height.equalTo(32)
        }
    }
    
    @objc private func clickDelButton(){
        delBlock?(index)
    }
}
